package com.atguigu.controller;

import com.atguigu.client.CartFeignClient;
import com.atguigu.client.ProductFeignClient;
import com.atguigu.entity.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class WebCartController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private CartFeignClient cartFeignClient;

    //1.加入购物车 addCart.html?skuId=27&skuNum=1
    @RequestMapping("addCart.html")
    public String addCart(@RequestParam Long skuId,@RequestParam Integer skuNum,  HttpServletRequest request) {
//        String userId = AuthContextHolder.getUserId(request);
//        String userTempId = AuthContextHolder.getUserTempId(request);
        //加入购物车
        cartFeignClient.addToCart(skuId,skuNum);
        //拿到商品的基本信息
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);
        return "cart/addCart";
    }

    //2.购物车列表
    @GetMapping("cart.html")
    public String cartIndex() {
        return "cart/index";
    }

}
